import 'package:flutter/material.dart';
import 'package:flutter_widget/src/widget/image/web_cache_Image.dart';

///背景效果的widget
class BackgroundWidget extends StatelessWidget {
  final String? bgImageUrl; //网络背景图
  final String? bgLocalImage; //本地背景图
  final Widget? bgWidget; //自定义widget，以上三选一
  final double? width; //宽
  final double? height; //高
  final Widget child; //上层widget
  final AlignmentGeometry? alignment; //对齐方式，默认居中

  BackgroundWidget({
    Key? key,
    this.bgImageUrl,
    this.bgLocalImage,
    this.bgWidget,
    required this.child,
    this.alignment,
    this.width,
    this.height,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    Widget body = Stack(
      alignment: alignment ?? Alignment.center,
      children: [
        Positioned.fill(
          child: bgWidget ??
              ((bgLocalImage?.isNotEmpty == true)
                  ? Image.asset(
                      bgLocalImage!,
                      fit: BoxFit.cover,
                    )
                  : WebCacheImage(
                      imageUrl: bgImageUrl,
                      fit: BoxFit.cover,
                    )),
        ),
        child,
      ],
    );
    if (width != null || height != null) {
      body = SizedBox(
        width: width,
        height: height,
        child: body,
      );
    }
    return body;
  }
}
